package com.newenergy.wisehome.ui.fragment;


import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.jude.easyrecyclerview.EasyRecyclerView;
import com.newenergy.wisehome.R;
import com.newenergy.wisehome.data.bean.Family;
import com.newenergy.wisehome.data.ormlite.dao.FamilyDao;
import com.newenergy.wisehome.ui.adapter.ShareAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import me.yokeyword.fragmentation.ISupportFragment;

/**
 *分享中家庭列表
 */
public class SelectFamilyFragment extends BaseFragment {


    private static String ARG_PAGE="arg_page";

    @BindView(R.id.btn_back)
    Button mBtnBack;
    @BindView(R.id.tv_back)
    TextView mTvBack;
    @BindView(R.id.tv_page)
    TextView mTvPage;
    @BindView(R.id.tv_select)
    TextView mTvSelect;
    @BindView(R.id.divider1)
    View mDivider1;
    @BindView(R.id.btn_next)
    Button mBtnNext;
    @BindView(R.id.recyclerView)
    EasyRecyclerView mRecyclerView;

    private ShareAdapter mAdapter;
    private LinearLayoutManager mLayoutManager;
    private List<Family> mFramilies;
    private int mcurrPosition = 0;
    private Family mCurrItem;
    private Dialog mDialog;
    private String mPage;

    public SelectFamilyFragment() {
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_select_framily;
    }


    public static ISupportFragment newInstance(String page) {
        Bundle args = new Bundle();
        args.putString(ARG_PAGE, page);
        SelectFamilyFragment fragment = new SelectFamilyFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void init(View rootView) {
        mFramilies = new ArrayList<>();
        initRecycleView();
        mPage = getArguments().getString(ARG_PAGE);
    }


    @Override
    public void onResume() {
        super.onResume();

        if (mPage.equals("hea_share")){
            mTvPage.setText(getString(R.string.hea_sharing));
        } else if (mPage.equals("group_share")) {
            mTvPage.setText(getString(R.string.lighting_sharing));
        }else if (mPage.equals("hea_handset")) {
            mTvPage.setText(getString(R.string.btn_share_handset));
        }

        FamilyDao familyDao = new FamilyDao(getContext());
        List<Family> allFamilies = familyDao.getAll();

        if (allFamilies.size()<=0){
            mBtnNext.setVisibility(View.INVISIBLE);
        }else {
            mBtnNext.setVisibility(View.VISIBLE);
        }
        mAdapter.refreshData(allFamilies);
        setCurrentItem();
    }



    private void setCurrentItem() {
        List<Family> datas = mAdapter.getAllData();

        if (datas.size() < 1) {
            mCurrItem = null;
            return;
        }
        mcurrPosition = mcurrPosition >= datas.size() ? 0 :  mcurrPosition;

        for (Family framily : datas) {
            framily.setChecked(false);
        }
        mCurrItem = mAdapter.getItem(mcurrPosition);
        mCurrItem.setChecked(true);
        mAdapter.notifyDataSetChanged();
    }



    private void initRecycleView() {
        Context context = getContext();
        mAdapter = new ShareAdapter(context);
        mRecyclerView.setAdapter(mAdapter);
        mLayoutManager = new LinearLayoutManager(context);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mAdapter.setOnItemClickListener(position -> {
            mcurrPosition = position;
            setCurrentItem();
        });
    }


    @OnClick({R.id.btn_back, R.id.tv_back, R.id.btn_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                doBack();
                break;
            case R.id.tv_back:
                doBack();
                break;
            case R.id.btn_next:

                if (mPage.equals("hea_share")){
                    start(SelectHEAFragment.newInstance(mCurrItem));
                } else if (mPage.equals("group_share")) {
//                    showBottomDialog();
                    start(SelectGroupFragment.newInstance(mCurrItem));
                }else if (mPage.equals("hea_handset")) {
//                    showBottomDialog();
                    start(HandsetShareFragment.newInstance(mCurrItem));
                }
                break;
        }
    }


    public void showBottomDialog() {
        View contenView = View.inflate(getContext(), R.layout.dialog_share, null);
        mDialog = new Dialog(getContext(), R.style.style_dialog);
        Window window = mDialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.getDecorView().setPadding(0, 0, 0, 0);

        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);

        Button shareQrcode = (Button) contenView.findViewById(R.id.btn_share_qrcode);

        Button shareHandset = (Button) contenView.findViewById(R.id.btn_share_handset);

        Button cancel = (Button) contenView.findViewById(R.id.btn_cancel);


        shareQrcode.setOnClickListener(v -> {
            mDialog.dismiss();
            if (mCurrItem == null) {
                myToast(getString(R.string.tip_please_add_family), true);
                return;
            }

            start(SelectGroupFragment.newInstance(mCurrItem));

        });

        shareHandset.setOnClickListener(v -> {
            mDialog.dismiss();
            if (mCurrItem == null) {
                myToast(getString(R.string.tip_please_add_family), true);
                return;
            }
            start(HandsetShareFragment.newInstance(mCurrItem));

        });
        cancel.setOnClickListener(v -> {
            mDialog.dismiss();
        });
        mDialog.setContentView(contenView);
        Window dialogWindow = mDialog.getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
        mDialog.show();

    }


}
